package com.sen.demo.thread;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @className: CustomerRunable
 * @Auther: jiesen.liu
 * @Date: 2019/7/22 14:13
 * @package: com.sen.demo.thread
 */
@Slf4j
public class CustomerRunable implements Runnable {

    private String name;
    private ReentrantLock lock;
    private static int num = 100;

    public CustomerRunable(String name, ReentrantLock lock) {
        this.name = name;
        this.lock = lock;
    }

    @Override
    public void run() {
        Thread.currentThread().setName(name);
        log.info("{}正在执行线程", name);
        lock.lock();
        if (num == 0) {
            log.info("{}正在执行线程----{}", name, num);
            try {
                Thread.sleep(1000000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        try {
            if (num> 0) {
                log.info("{}正在执行线程----{}", name, num--);
            }
        }finally {
            lock.unlock();
        }

    }
}
